/**
 * 
 */
package info.niwota.ezb;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;
import android.text.TextUtils;

/**
 * @author qiangli
 * 
 */
public final class Preferences {
	private static final String BP_POS = "bp.pos";

	private static final String BP_SCALED = "bp.scaled";

	private static final String BP_SCALE = "bp.scale";

	private static final String BP_SCROLL_XY = "bp.scroll.xy";

	private static final String BP_SEND_BODY = "settings.send.body";

	private static final String BP_SEND_EMAILS = "settings.send.email";
	
	private static final String BP_SEND_CC = "settings.send.cc";
	
	private static final String BP_SEND_BCC = "settings.send.bcc";

	private static final String BP_SEND_SUBJECT = "settings.send.subject";
	
	//
	private static final String BP_POWER_FEATURE_TOGGLE = "settings.power.feature.toggle";
	
	private static final String BP_POWER_FULLBRIGHT_TOGGLE = "settings.power.fullbright.toggle";
	
	
	private static final String BP_TAXI_PHONE = "settings.taxi.phone";
	
	//private static final String BP_WAKEUP_TOGGLE = "settings.wakeup.toggle";
	
	//private static final String BP_WAKEUP_THRESHOLD = "settings.wakeup.threshold";

	private static final String BP_URIS = "bp.uris";

	private static final String EMAIL_SEP = ",";

	private static final String SEP = "\n";

	public static final int SPEED_THRESHOLD = 300;

	private static final String BP_RUNNING_STATUS = "bp.running.status";

	public static String getBody(Context ctx) {
		try {
			final SharedPreferences p = PreferenceManager
					.getDefaultSharedPreferences(ctx);
			String defval = ctx.getString(R.string.msg_body_default);
			String s = p.getString(BP_SEND_BODY, defval);
			return s;
		} catch (Exception e) {
		}
		return null;
	}

	public static String[] getEmails(Context ctx) {
		try {
			final SharedPreferences p = PreferenceManager
					.getDefaultSharedPreferences(ctx);
			String s = p.getString(BP_SEND_EMAILS, null);
			if (!TextUtils.isEmpty(s)) {
				String[] a = s.split(EMAIL_SEP);
				return a;
			}
		} catch (Exception e) {
		}
		return null;
	}
	
	public static String[] getCc(Context ctx) {
		try {
			final SharedPreferences p = PreferenceManager
					.getDefaultSharedPreferences(ctx);
			String s = p.getString(BP_SEND_CC, null);
			if (!TextUtils.isEmpty(s)) {
				String[] a = s.split(EMAIL_SEP);
				return a;
			}
		} catch (Exception e) {
		}
		return null;
	}
	
	public static String[] getBcc(Context ctx) {
		try {
			final SharedPreferences p = PreferenceManager
					.getDefaultSharedPreferences(ctx);
			String s = p.getString(BP_SEND_BCC, null);
			if (!TextUtils.isEmpty(s)) {
				String[] a = s.split(EMAIL_SEP);
				return a;
			}
		} catch (Exception e) {
		}
		return null;
	}

	public static int getPos(Context ctx) {
		final SharedPreferences p = PreferenceManager
		.getDefaultSharedPreferences(ctx);
		return getPos(p);
	}

	private static int getPos(SharedPreferences p) {
		try {
			String s = p.getString(BP_POS, null);
			if (!TextUtils.isEmpty(s)) {
				return Integer.parseInt(s);
			}
		} catch (Exception e) {
		}
		return 0;
	}
	
	public static float getScale(Context ctx) {
		try {
			final SharedPreferences p = PreferenceManager
					.getDefaultSharedPreferences(ctx);
			String s = p.getString(BP_SCALE, null);
			if (!TextUtils.isEmpty(s)) {
				return Float.parseFloat(s);
			}
		} catch (Exception e) {
		}
		return 1.0f;
	}

	public static float[] getScrollXY(Context ctx) {
		try {
			final SharedPreferences p = PreferenceManager
					.getDefaultSharedPreferences(ctx);
			String s = p.getString(BP_SCROLL_XY, null);
			if (!TextUtils.isEmpty(s)) {
				String[] a = s.split(SEP);
				float[] v = new float[18];
				for (int i = 0; i < v.length; i++) {
					v[i] = Float.parseFloat(a[i]);
				}
				return v;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static String getSubject(Context ctx) {
		try {
			final SharedPreferences p = PreferenceManager
					.getDefaultSharedPreferences(ctx);
			String defval = ctx.getString(R.string.msg_subject_default);
			String s = p.getString(BP_SEND_SUBJECT, defval);
			return s;
		} catch (Exception e) {
		}
		return null;
	}

	public static String[] getUris(Context ctx) {
		final SharedPreferences p = PreferenceManager
		.getDefaultSharedPreferences(ctx);
		return getUris(p);
	}
	
	private static String[] getUris( SharedPreferences p) {
		try {
			String s = p.getString(BP_URIS, null);
			if (!TextUtils.isEmpty(s)) {
				return s.split(SEP);
			}
		} catch (Exception e) {
		}
		return null;
	}

	public static void addUri(Context ctx, String uri) {
		// add the uri at the current position
		try {
			final SharedPreferences p = PreferenceManager
					.getDefaultSharedPreferences(ctx);
			int pos = getPos(p);
			String[] a = getUris(ctx);
			List<String> uris = new ArrayList<String>();
			//do this the hard way as we need to keep the order and do add
			//otherwise list.add throws unsupported operation exception
			if (a != null) {
				for (int i = 0; i < a.length; i++) {
					uris.add(a[i]);
				}
			}
			int size = uris.size();
			pos = (pos >= size ? size - 1 : pos);
			pos = (pos < 0 ? 0 : pos);
		
			uris.add(pos, uri);
			//
			putUris(p, uris.toArray(new String[0]));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void putPos(Context ctx, int pos) {
		final SharedPreferences p = PreferenceManager
				.getDefaultSharedPreferences(ctx);
		p.edit().putString(BP_POS, pos + "").commit();
	}

	public static void putScale(Context ctx, float scale) {
		final SharedPreferences p = PreferenceManager
				.getDefaultSharedPreferences(ctx);
		p.edit().putString(BP_SCALE, scale + "").commit();

	}
	
	public static void putScaled(Context ctx, boolean scaled) {
		final SharedPreferences p = PreferenceManager
				.getDefaultSharedPreferences(ctx);
		p.edit().putBoolean(BP_SCALED, scaled).commit();
	}

	public static void putScrollXY(Context ctx, float[] values) {
		final SharedPreferences p = PreferenceManager
				.getDefaultSharedPreferences(ctx);
		StringBuilder sb = new StringBuilder();
		if (values != null) {
			for (int i = 0; i < values.length; i++) {
				sb.append(values[i]);
				sb.append(SEP);
			}
		}
		p.edit().putString(BP_SCROLL_XY, sb.toString()).commit();
	}

	public static void putUris(Context ctx, String[] uris) {
		final SharedPreferences p = PreferenceManager
				.getDefaultSharedPreferences(ctx);
		putUris(p, uris);
	}

	synchronized private static void putUris(SharedPreferences p, String[] uris) {
		Editor e = p.edit();
		if (uris == null || uris.length == 0) {
			e.putString(BP_URIS, "").commit();
		} else {
			StringBuilder sb = new StringBuilder();
			for (int i = 0; i < uris.length; i++) {
				sb.append(uris[i]);
				sb.append(SEP);
			}
			e.putString(BP_URIS, sb.toString()).commit();
		}
	}
	
	public static boolean isScaled(Context ctx) {
		final boolean defvalue = false;
		try {
			final SharedPreferences pref = PreferenceManager
			.getDefaultSharedPreferences(ctx);
			return pref.getBoolean(BP_SCALED, defvalue);
		} catch (Exception e) {
		}
		return defvalue;
	}
	
	//
	public static boolean enableFullbright(Context ctx) {
		final boolean defval = true;
		try {
			final SharedPreferences pref = PreferenceManager
					.getDefaultSharedPreferences(ctx);
			return pref.getBoolean(BP_POWER_FULLBRIGHT_TOGGLE, defval);	
		} catch (Exception e) {
			e.printStackTrace();
		}
		return defval;
	}
	
	public static boolean enablePowerFeature(Context ctx) {
		final boolean defval = true;
		try {
			final SharedPreferences pref = PreferenceManager
					.getDefaultSharedPreferences(ctx);
			
			return pref.getBoolean(BP_POWER_FEATURE_TOGGLE, defval);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return defval;
	}
	
	public static boolean getRunningStatus(Context ctx) {
		final boolean defvalue = false;
		try {
			final SharedPreferences pref = PreferenceManager
					.getDefaultSharedPreferences(ctx);
			return pref.getBoolean(BP_RUNNING_STATUS, defvalue);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return defvalue;
	}
	
	public static void updateRunningStatus(Context ctx, boolean b) {
		try {
			final SharedPreferences p = PreferenceManager
			.getDefaultSharedPreferences(ctx);
			p.edit().putBoolean(BP_RUNNING_STATUS, b).commit();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static String getPhone(Context ctx) {
		try {
			final SharedPreferences p = PreferenceManager
					.getDefaultSharedPreferences(ctx);
			String s = p.getString(BP_TAXI_PHONE, null);
			return s;
		} catch (Exception e) {
		}
		return null;
	}
	//
	private Preferences() {
	}
}
